<template>
  <div class="drug-ware-info">
    <el-tabs class="info-tabs" :value="operation+''">
      <el-tab-pane
        v-if="operation == 0"
        :label="type == 0 ? '新增入库':'新增采购'"
        name="0"
      >
        <infoForm
          @onChangeTotalPrice="onChangeTotalPrice"
          :warehousingId="warehousingId"
          :operation="operation"
          @success="onSuccess"
          ref="infoForm"
          :type="type"
        />
      </el-tab-pane>
      <el-tab-pane
        v-if="operation == 1 || operation == 4 || (operation == 7 && type == 0)"
        :name="operation+''"
        :label="type == 0 ? '入库编辑':'采购编辑'"
      >
        <infoForm
          @onChangeTotalPrice="onChangeTotalPrice"
          :warehousingId="warehousingId"
          :operation="operation"
          @success="onSuccess"
          ref="infoForm"
          :type="type"
        />
      </el-tab-pane>
      <el-tab-pane
        v-if="operation == 2 || operation == 3 || operation == 5 || operation == 6 || (operation == 7 && type == 1)"
        :name="operation+''"
        :label="type == 0 ? '入库明细':'采购明细'"
      >
        <infoForm
          @onChangeTotalPrice="onChangeTotalPrice"
          :warehousingId="warehousingId"
          :operation="operation"
          @success="onSuccess"
          :readOnly="true"
          ref="infoForm"
          :type="type"
        />
      </el-tab-pane>
    </el-tabs>
    <div class="dialog-footer" style="margin-top:20px">
      <span style="padding-right: 10px;">订单总价：{{ formatMoney(totalPrice, 2) }}</span>
      <template v-if="(operation == 1 || operation == 0) && type == 1">
        <el-button :loading="loading" @click="onSubmit(1)">保存</el-button>
        <el-button :loading="loading" @click="onSubmit(2)">提交</el-button>
      </template>
      <template v-if="operation == 2">
        <el-button :loading="loading" @click="onSubmit(4)" v-permission="['admin', 'cgjh:nopass']">退回</el-button>
        <el-button :loading="loading" @click="onSubmit(6)" v-permission="['admin', 'cgjh:pass']">通过</el-button>
      </template>
      <template v-if="operation == 3 && type == 0">
        <el-button :loading="loading" @click="onOutWareHousing">导出入库单</el-button>
      </template>
      <template v-if="operation == 3 && type == 1">
        <el-button :loading="loading" @click="onOutWareHousing">导出采购单</el-button>
      </template>
      <template v-if="operation == 4">
        <el-button :loading="loading" @click="onSubmit(1)">保存订单</el-button>
        <el-button :loading="loading" @click="onSubmit(2)">提交订单</el-button>
      </template>
      <template v-if="operation == 6">
        <el-button :loading="loading" @click="onSubmit(7)">确认到货</el-button>
      </template>
      <template v-if="(operation == 7 || operation == 0) && type == 0">
        <el-button :loading="loading" @click="onSubmit(7)">保存</el-button>
        <el-button :loading="loading" @click="onSubmit(3)">入库</el-button>
      </template>
      <el-button :loading="loading" @click="$emit('close')">返回</el-button>
    </div>
  </div>
</template>

<script>
import { add, outWareHousing } from "@/api/storageRoom/drugWareHousing";
import { formatMoney } from "@/utils/index";
import infoForm from "./infoForm.vue";

export default {
  name: "drugWareInfo",
  inheritAttrs: false,
  props: {
    operation: {
      type: Number,
      default: 0
    },
    warehousingId: {
      type: Number,
      default: 0
    },
    // 0-入库, 1-采购计划
    type: {
      type: Number,
      default: 0
    }
  },
  components: { infoForm },
  data() {
    return {
      successMsg: {
        1: "保存成功！",
        3: "入库成功！",
        2: "操作成功! ",
        4: "操作成功! ",
        5: "操作成功! ",
        6: "操作成功! ",
        7: "操作成功! ",
      },
      loading: false,
      totalPrice: 0,
    };
  },
  methods: {
    formatMoney,
    onSubmit(status) {
      let postParam = this.$refs.infoForm.getPostParam();
      let refundContent = this.$refs.infoForm.refundContent;
      // 退回理由单独处理
      if(status == 4 && !refundContent) {
        this.$message.error('请填写退回理由!');
        return;
      } else if(status == 4) {
        if(postParam.refundContent) {
          postParam.refundContent = postParam.refundContent + ',' + refundContent;
        } else {
          postParam.refundContent = refundContent;
        }
      }
      
      if(postParam.list.length === 0) {
        this.$message.error('您还未添加药品');
        return;
      }
      this.$refs.infoForm.validator((errors, fileds) => {
        if(errors) {
          this.$message.error('您还有未填写完成的信息!');
          console.log("............. error fileds: ", fileds);
        } else {
          // 1：待提交，2待审核，3：已入库，4：已驳回，5已取消，6已审核
          postParam.status = status;
          this._submit(postParam, status);
        }
      });
    },
    _submit(postParam, status) {
      // console.log(postParam);
      this.loading = true;
      add(postParam).then(() => {
        this.loading = false;
        this.$notify({
          title: this.successMsg[status],
          type: "success"
        });
        this.onSuccess();
      }).catch(() => {
        this.loading = false;
      });
    },
    onChangeTotalPrice(value) {
      this.totalPrice = value;
    },
    // 导出入库/采购单
    onOutWareHousing() {
      let postParam = this.$refs.infoForm.getPostParam();
      let id = postParam.id;
      outWareHousing({id}, this.type);
    },
    onSuccess(show) {
      this.$emit("success", show);
    }
  }
}
</script>

<style>
.drug-ware-info {
  height: 600px;
  width: 100%;
}

.drug-ware-info .info-tabs {
  height: 570px;
  width: 100%;
}

.drug-ware-info .info-tabs .el-tabs__content {
  height: calc(100% - 40px);
}

.drug-ware-info .info-tabs .el-tabs__content .el-tab-pane {
  height: 100%;
}

.drug-ware-info .span-20 {
  margin-left: 15px;
}

.drug-ware-info .button,
.drug-ware-info .dialog-footer {
  text-align: right;
}

.drug-ware-info .info-row {
  margin-bottom: 0;
}

.info-table tr td {
  padding: 5px 0 0 15px;
}
</style>